package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.Customer;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.CustomerService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 潜在客户资料管理 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    //注入
    @Autowired
	CustomerService customerService;
    //创建日志Logger的对象
    Logger logger = Logger.getLogger(CustomerController.class);

    /**
     * 返回页面潜在客户的页面
     * @return  潜在客户的页面
     */
    @RequestMapping("/adminInfo")
    public String adminInfo(){
        return "admin_info";
    }



    /**
     * 添加潜在客户资料
     * @param
     * @return 添加成功返回true 添加失败返回false
     */
    @RequestMapping("/insertCustomer")
    @ResponseBody
    public boolean insertCustomer(String param,HttpSession session){
        //使用反射吧json字符串转换成对象
        Customer customer = JSONObject.parseObject(param, Customer.class);
        //转换时间本来是时间戳类型的
        customer.setCreateTime(new Date().getTime());
		long areaid= ((User)session.getAttribute("user")).getAreaId();
        customer.setAreaid(areaid);
        boolean flag = customerService.insert(customer);
        String msg =  flag?"添加成功"+customer:"添加失败";
        logger.info(msg);
        return flag;
    }

    /**
     * 查询所有潜在客户资料(分页查询)
     * @param voPage
     * @return 返回潜在客户资料
     */
    //查询所有
    @RequestMapping("/selectCustomer")
    @ResponseBody
    public ResultPage<Customer> selectCustomer(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        long areaid= ((User)session.getAttribute("user")).getAreaId();
        ResultPage<Customer> resultPage= customerService.selectMyPage(map,voPage,areaid,admin);
        return resultPage;
}

    /**
     * 删除潜在客户资料
    */
    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public boolean deleteCustomer(Long customerId){
        return customerService.deleteById(customerId);
    }


    @RequestMapping("/updateCustomer")
    @ResponseBody
    public String updateCustomer(String param){
        Customer customer = JSONObject.parseObject(param, Customer.class);
        if(customerService.updateById(customer)){

            return "修改成功";
        }else{
            return "修改失败";
        }
    }



    //返回添加页面
    @RequestMapping("/client_add")
    public String clientAdd(){
        return "/Alert/client_add";
    }

    //返回编辑的页面
    @RequestMapping("/client_edit")
    public String clientEdit(){
        return "/Alert/client_edit";
    }



}
